package logmanager.job;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.rmi.RemoteException;
import java.rmi.server.UnicastRemoteObject;
import java.util.ArrayList;
import java.util.Properties;

import logmanager.proxy.IDiscoveryLogManager;

//classe de gestion des logs implementant l'interface squelette
public class LogManager extends UnicastRemoteObject implements IDiscoveryLogManager{
	private static final long serialVersionUID = -3500307278797950893L;
	
	private ArrayList<LogJob> logs;
	
	public LogManager() throws RemoteException{
		if (System.getSecurityManager() == null)
            System.setProperty("java.security.policy", "allpolicy.txt");
		logs = new ArrayList<LogJob>();
		System.out.println("Demarage logmanager");
	}
	
	public int addLogJobByName(String fileName) throws RemoteException{
		File f = new File(getResourcesPath() + "\\" + fileName);
		if(!f.exists()){
			return 1;
		}
		try {
			logs.add(new LogJob(getResourcesPath() + "\\" + fileName));
		} catch (FileNotFoundException e) {
			return 1;
		}
		return 0;
	}
////////////////////	
	public void removeLog(int index) throws RemoteException{
		logs.remove(index);
	}
	public int removeLogByName(String fileName) throws RemoteException{
		for (int i = 0; i < logs.size(); i++) {
			if(logs.get(i).getFileName().toLowerCase().equals(fileName.toLowerCase())){
				logs.remove(i);
				return i;
			}
		}
		return -1;
	}
////////////////////	
	public String[] getDataHeader(int index)  throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return null;
		return l.getDataHeader();
	}
	public String[] getDataHeaderByName(String fileName)  throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return null;
		return l.getDataHeader();
	}
////////////////////	
	public String[][] getNextData(int index) throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return null;
		return dogetNextDatas(l, "", "");
	}
	public String[][] getNextDataByName(String fileName) throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return null;
		return dogetNextDatas(l, "", "");
	}
	public String[][] getNextDataBySearch(int index, String field, String value) throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return null;
		return dogetNextDatas(l, field, value);
	}
	public String[][] getNextDataByNameBySearch(String fileName, String field, String value) throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return null;
		return dogetNextDatas(l, field, value);
	}
////////////////////
	public String[][] getPrevData(int index) throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return null;
		return dogetPrevDatas(l, "", "");
	}
	public String[][] getPrevDataByName(String fileName) throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return null;
		return dogetPrevDatas(l, "", "");
	}
	public String[][] getPrevDataBySearch(int index, String field, String value) throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return null;
		return dogetPrevDatas(l, field, value);
	}
	public String[][] getPrevDataByNameBySearch(String fileName, String field, String value) throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return null;
		return dogetPrevDatas(l, field, value);
	}
//////////////////
	public String[][] getData(int index) throws RemoteException{
		return getLogJob(index).getData();
	}
	public String[][] getDataByName(String fileName) throws RemoteException{
		return getLogJob(fileName).getData();
	}
////////////////////	
	public int setFilter(int index, String field, String value) throws RemoteException{
		if(index > getSize())
			return 1;
		getLogJob(index).setFilter(field, value);
		return 0;
	}
	public int setFilterByName(String fileName, String field, String value) throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return 1;
		l.setFilter(field, value);
		return 0;
	}
////////////////////
	public String getFilterField(int index) throws RemoteException{
		if(index > getSize())
			return null;
		return getLogJob(index).getFilter().getField();
	}
	public String getFilterFieldByName(String fileName) throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return null;
		return l.getFilter().getField();
	}
	public String getFilterValue(int index) throws RemoteException{
		if(index > getSize())
			return null;
		return getLogJob(index).getFilter().getValue();
	}
	public String getFilterValueByName(String fileName) throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return null;
		return l.getFilter().getValue();
	}
////////////////////
	public int getSize() throws RemoteException{
		return logs.size();
		//return 15;
	}
////////////////////	
	public int setReadSize(int index, int readSize)  throws RemoteException{
		if(index > getSize())
			return 1;
		getLogJob(index).setReadSize(readSize);
		return 0;
	}
	public int setReadSizeByName(String fileName, int readSize)  throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return 1;
		l.setReadSize(readSize);
		return 0;
	}
////////////////////	
	public int getReadSize(int index)  throws RemoteException{
		if(index > getSize())
			return -1;
		return getLogJob(index).getReadSize();
	}
	public int getReadSizeByName(String fileName)  throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return -1;
		return l.getReadSize();
	}
////////////////////	
	public int getLineReadCount(int index) throws RemoteException{
		if(index > getSize())
			return -1;
		return getLogJob(index).getLineCount();
	}
	public int getLineReadCountByName(String fileName) throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return -1;
		return getLogJob(fileName).getLineCount();
	}
////////////////////	
	public int getPosition(int index)  throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return -1;
		return l.getPosition();
	}
	public int getPositionByName(String fileName)  throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return -1;
		return l.getPosition();
	}
////////////////////	
	public int getFirstsLineSize(int index)  throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return -1;
		return l.getFirstsLineSize();
	}
	public int getFirstsLineSizeByName(String fileName)  throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return -1;
		return l.getFirstsLineSize();
	}
////////////////////	
	public int getLogSizeByName(String fileName)  throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return 0;
		return l.getLogSize();
	}
	public int getLogSize(int index)  throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return 0;
		return l.getLogSize();
	}
////////////////////	
	public String getMaxDateByName(String fileName)  throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return "";
		return l.getMaxDate();
	}
	public String getMaxDate(int index)  throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return "";
		return l.getMaxDate();
	}
////////////////////	
	public String getMinDateByName(String fileName)  throws RemoteException{
		LogJob l = getLogJob(fileName);
		if(l == null)
			return "";
		return l.getMinDate();
	}
	public String getMinDate(int index)  throws RemoteException{
		LogJob l = getLogJob(index);
		if(l == null)
			return "";
		return l.getMinDate();
	}
////////////////////
	public String getResourcesPath()  throws RemoteException{
		Properties prop = new Properties();
		FileInputStream fin;
		try {
			fin = new FileInputStream("setting/conf.properties");
			prop.load(fin);
			
			String path = prop.getProperty("resources_patht");
			
			fin.close();
			return path;
		} catch (Exception e) {
			return "resources";
		}
	}
	public void setResourcesPath(String path)  throws RemoteException{
	//	System.out.println(path);
		try {
			Properties prop = new Properties();
			
			prop.setProperty("resources_patht", path);
			
			FileOutputStream out = new FileOutputStream("setting/conf.properties");
			prop.store(out, "resources's path");
			out.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		
	}
	public String[] getFiles() throws RemoteException{
		File f = new File(getResourcesPath());
		File []f1 = f.listFiles();
		String [] s = new String[f1.length];
		for (int i = 0; i < s.length; i++) {
			s[i] = f1[i].getPath();
		}
		return s;
	}
////////////////////	
	private LogJob getLogJob(String fileName){
		for (int j = 0; j < logs.size(); j++) {
			if(logs.get(j).getFileName().toLowerCase().equals(fileName.toLowerCase()))
				return logs.get(j);
		}
		return null;
	}
	private LogJob getLogJob(int i){
		if(i > logs.size())
			return null;
		return logs.get(i);
	}	
	private String[][] dogetNextDatas(LogJob l, String field, String value){
		if(field.equals("") && value.equals(""))
			l.getNextDatas();
		else
			l.getNextDatas(field, value);
		return l.getData();
	}
	private String[][] dogetPrevDatas(LogJob l, String field, String value){
		if(field.equals("") && value.equals(""))
			l.getPrevDatas();
		else
			l.getPrevDatas(field, value);
		return l.getData();
	}
////////////////////	
	public void print(String [][]data){
		System.out.println("data");
		for (int i = 0; i < data.length; i++) {
			for (int j = 0; j < data[i].length; j++) {
				System.out.print(" " + data[i][j]);
			}
			System.out.println();
		}
	}
////////////////////	
}
